package com.sakura.mall.ware.wareInfo.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.ware.wareInfo.domain.entity.WareInfoEntity;
import com.sakura.mall.ware.wareInfo.domain.request.AddWareInfoRequest;
import com.sakura.mall.ware.wareInfo.domain.request.UpdateWareInfoRequest;
import com.sakura.mall.ware.wareInfo.domain.request.WareInfoPageRequest;
import com.sakura.mall.ware.wareInfo.domain.vo.WareInfoVo;
import com.sakura.mall.ware.wareInfo.mapper.WareInfoMapper;
import com.sakura.mall.ware.wareInfo.service.WareInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 仓库信息 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class WareInfoServiceImpl extends ServiceImpl<WareInfoMapper, WareInfoEntity> implements WareInfoService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddWareInfoRequest request) {
        // todo 其他校验逻辑
        WareInfoEntity wareInfo = request.convert(WareInfoEntity.class);
        this.save(wareInfo);
    }

    @Override
    public Page<WareInfoVo> queryPage(WareInfoPageRequest request) {
        IPage<WareInfoEntity> qp = request.toPage();
        LambdaQueryWrapper<WareInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<WareInfoVo> ret = qp.convert(entity -> entity.convert(WareInfoVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateWareInfoRequest request) {
        WareInfoEntity entity = request.convert(WareInfoEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

